//
//  ClassesViewModel.swift
//  scfriend
//
//  Created by wulanzhou on 2017/7/18.
//  Copyright © 2017年 wulanzhou. All rights reserved.
//

import Foundation
import Alamofire
import SwiftyJSON

class ClassesViewModel: NSObject{
    
    var data:[SCClass] = [SCClass]()
   
    
    func loadClassesList(userId:String,completion:(() ->Void)?) {
        let urlStr:String = kSC_SERVER_URL + "classes/list/format/json?user_id="+userId
        
        weak var weakSELF = self
        
        Alamofire.request(urlStr, method: .get).responseJSON { response in
            switch response.result.isSuccess {
            case true:
                let mul:NSMutableArray = NSMutableArray.init(capacity: 0)
                if let value = response.result.value {
                    let json = JSON(value)
                    //print(response.result.value)
                    if let sourceData:Array = json["data"].array {
                       
                        
                        for item in sourceData {
                            if let it = item.dictionary {
                                let entity:SCClass = SCClass()
                                entity.mapper(map: it)
                                mul.add(entity)
                            }
                            
                        }
                        
                    }
                }
                weakSELF?.data = (mul as? [SCClass])!   
            case false:
                
                print(response.result.error ?? "请求发生失败")
            }
            completion?()
        }
    }
    
    
}
